import scrapy
from scrapy.loader import ItemLoader
from scrapy.loader.processors import TakeFirst
from scrapy.exceptions import DropItem
from tzj.items import TzjItem
import arrow


class Enterprise_spider(scrapy.Spider):
    name = 'enterprise'
    handle_httpstatus_list = [404]
    # download_delay = 2
    # strat_urls = []
    def start_requests(self):
        url = 'http://zdb.pedaily.cn/enterprise/show%s/'
        for _id in range(10000,35050):
            yield scrapy.Request(url=url%_id, callback=self.parse,meta={'_id':_id})

    def parse(self, response):
        proc = TakeFirst()
        l = ItemLoader(item=TzjItem(), response=response)
        l.add_value('_id',response.meta['_id'])

        l.add_value('_update',arrow.now().floor('day').datetime)
        if response.status == 404:
            l.add_value('_status',404)
            return l.load_item()

        l.add_value('_status',200)
        l.add_css('full_name','.box-fix-l > .info h1 ::text',)
        l.add_css('name','.box-fix-l > .info h1 em ::text',)
        l.add_css('en_name','.box-fix-l > .info h2 ::text',)
        l.add_css('desc','div#desc p ::text',)
        l.add_css('contact','#contact p ::text',)

        for li in response.css('.box-fix-l > .info li'):
            span = li.css('::text').extract()
            l.add_value(span[0].strip(' ：'),span[1])

        for li in response.css('#inv-box li')[1:]:

              dic = {}
              dic['date'] = li.css('span.time ::text').extract_first()
              dic['money'] = '%s %s'%(li.css('span.d ::text').extract_first(),li.css('span.m ::text').extract_first())
              dic['series'] = li.css('span.r ::text').extract_first()
              dic['investors'] = li.css('dt.group a ::text').extract()
              l.add_value('fundinground',dic)
        try:
            return l.load_item()
        except Exception as e:
            self.logger.info('parse error : %s'%response.meta['_id'])
            raise DropItem
